#include <sdk.h>
#include "compilerCgenerator.h"
#include <wx/intl.h>
#include "compilerfactory.h"
#include "compiler.h"
#include "manager.h"
#include "configmanager.h"
#include "logmanager.h"
#include "macrosmanager.h"
#include "scriptingmanager.h"

CompilerCGenerator::CompilerCGenerator() {
  m_VerStr = wxEmptyString;
}

CompilerCGenerator::~CompilerCGenerator() {
}

wxString CompilerCGenerator::SetupIncludeDirs( Compiler* compiler, ProjectBuildTarget* target ) {
  wxString result = CompilerCommandGenerator::SetupIncludeDirs( compiler, target );
  m_VerStr = compiler->GetVersionString();
  wxString pch_prepend = wxEmptyString;
  long gcc_major = 4;
  if( !m_VerStr.IsEmpty() ) {
    m_VerStr.BeforeFirst( '.' ).ToLong( &gcc_major );
  }
  bool HasPCH = false;
  if( target &&
      target->GetParentProject()->GetModeForPCH() == pchObjectDir ) {
    wxArrayString includedDirs;
    for( ProjectFile *f : target->GetParentProject()->GetFilesList() ) {
      if( f->compile && FileTypeOf( f->relativeFilename ) == ftHeader ) {
        wxFileName fn( f->GetObjName() );
        wxString objName = ( compiler->GetSwitches().UseFlatObjects ) ? fn.GetFullName() : fn.GetFullPath();
        wxString dir = wxFileName( target->GetObjectOutput() + _T( '/' ) + objName ).GetPath();
        if( includedDirs.Index( dir ) == wxNOT_FOUND ) {
          includedDirs.Add( dir );
          QuoteStringIfNeeded( dir );
          if( gcc_major < 4 ) {
            pch_prepend << compiler->GetSwitches().includeDirs << dir << _T( ' ' );
          } else
          { pch_prepend << _T( "-iquote" ) << dir << _T( ' ' ); }
        }
        HasPCH = true;
      }
    }
    if( gcc_major < 4 ) {
      pch_prepend << _T( "-I- " );
    }
    int count = ( int )includedDirs.GetCount();
    for( int i = 0; i < count; ++i ) {
      QuoteStringIfNeeded( includedDirs[i] );
      pch_prepend << compiler->GetSwitches().includeDirs << includedDirs[i] << _T( ' ' );
    }
    pch_prepend << _T( "-I. " );
  }
  if( HasPCH ) {
    result.Prepend( pch_prepend );
  }
  return result;
}
